<template>
  <view>
    <view class="goods_list">
      <view v-for="(item, i) in goods_list" :key="i" @click="gotoDetail(item)">
        <my-goods :goods="item"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        goods_list: [],
        total: 0,
        isload: false
      }
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''

      this.getGoodsLists()
    },
    methods: {
      async getGoodsLists(cd) {
        this.isload = true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        this.isload = false
        cd && cd()
        if (res.meta.status !== 200) return uni.$showMsg()

        this.goods_list = [...this.goods_list, ...res.message.goods]
        this.total = res.message.total
      },
      gotoDetail(item){
        uni.navigateTo({
          url: '../goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
    },
    onReachBottom() {
      if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
        return
      }
      if (this.isload) return;
      this.queryObj.pagenum++;
      this.getGoodsLists()
    },
    onPullDownRefresh() {
      this.queryObj.pagenum = 1
      this.total = 0
      this.isload = false
      this.goods_list = []
      this.getGoodsLists(() => {
        uni.stopPullDownRefresh()
      })
    }
  }
</script>

<style lang="scss">

</style>
